package tx.bj12320.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import cn.org.rapid_framework.beanutils.BeanUtils;
import cn.org.rapid_framework.web.scope.Flash;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;

import java.io.File;
import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import tx.bj12320.model.*;
import tx.bj12320.dao.*;
import tx.bj12320.service.*;
import tx.bj12320.util.WebUtils;
import tx.bj12320.vo.query.*;


public class DocInfoAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	//forward paths
	protected static final String QUERY_JSP = "/pages/DocInfo/query.jsp";
	protected static final String LIST_JSP= "/pages/DocInfo/list.jsp";
	protected static final String CREATE_JSP = "/pages/DocInfo/create.jsp";
	protected static final String EDIT_JSP = "/pages/DocInfo/edit.jsp";
	protected static final String SHOW_JSP = "/pages/DocInfo/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/DocInfo/list.do";

	private DocInfoManager docInfoManager;

	private DocInfo docInfo;
	Long id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			docInfo = new DocInfo();
		} else {
			docInfo = (DocInfo)docInfoManager.getById(id);
		}
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setDocInfoManager(DocInfoManager manager) {
		this.docInfoManager = manager;
	}

	public Object getModel() {
		return docInfo;
	}

	public void setDocInfoId(Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 执行搜索 */
	public String list() {
		DocInfoQuery query = newQuery(DocInfoQuery.class,DEFAULT_SORT_COLUMNS);

		Page page = docInfoManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}

	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}

	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	private File docFile;
	private String docFileContentType;
	private String docFileFileName;


	public File getDocFile() {
		return docFile;
	}

	public void setDocFile(File docFile) {
		this.docFile = docFile;
	}

	public String getDocFileContentType() {
		return docFileContentType;
	}

	public void setDocFileContentType(String docFileContentType) {
		this.docFileContentType = docFileContentType;
	}

	public String getDocFileFileName() {
		return docFileFileName;
	}

	public void setDocFileFileName(String docFileFileName) {
		this.docFileFileName = docFileFileName;
	}

	/** 保存新增对象 */
	public String save() {

		docInfoManager.save(docInfo);

		if (docFile != null) {
			String fileFormat = WebUtils.getExtention(docFileFileName);
			String newFileName =docInfo.getDocInfoId()+"_"+docFileFileName;
			File newFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/pages/DocInfo/files/") + newFileName);
			WebUtils.copy(docFile, newFile);

			docInfo.setDocFileName(newFileName);
		}
		docInfoManager.saveOrUpdate(docInfo);

		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION;
	}

	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}

	/**保存更新对象*/
	public String update() {
		if (docFile != null) {
			String fileFormat = WebUtils.getExtention(docFileFileName);
			String newFileName =docInfo.getDocInfoId()+ fileFormat;
			File newFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/doc-info-files") + "/" + newFileName);
			WebUtils.copy(docFile, newFile);

			docInfo.setDocFileName(newFileName);
		}
		docInfoManager.update(this.docInfo);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}

	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			Long id = new Long((String)params.get("docInfoId"));
			docInfoManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}

}
